﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Xml.Linq;
using Model;

namespace DAL
{
    public class LogRepository
    {
        private readonly string _connectionString = "Data Source=test.db;Version=3;";

        // 插入日志记录
        public void AddLog(Log log)
        {
            using (var conn = new SQLiteConnection(_connectionString))
            {
                conn.Open();
                string query = "INSERT INTO Log (ActionType, EntityName, EntityID, Timestamp, Description) VALUES (@actionType, @entityName, @entityID, @timestamp, @description)";
                using (var cmd = new SQLiteCommand(query, conn))
                {
                    cmd.Parameters.AddWithValue("@actionType", log.ActionType);
                    cmd.Parameters.AddWithValue("@entityName", log.EntityName);
                    cmd.Parameters.AddWithValue("@entityID", log.EntityID);
                    cmd.Parameters.AddWithValue("@timestamp", log.Timestamp);
                    cmd.Parameters.AddWithValue("@description", log.Description);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        // 查询所有日志
        public List<Log> GetAllLogs(string actionType)
        {
            var logs = new List<Log>();
            using (var conn = new SQLiteConnection(_connectionString))
            {
                conn.Open();
                string query = "SELECT * FROM Log";
                if (actionType != "")
                {
                    query = "SELECT * FROM Log WHERE ActionType LIKE @actionType";
                }
                using (var cmd = new SQLiteCommand(query, conn))
                {
                    cmd.Parameters.AddWithValue("@actionType", actionType);
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var log = new Log
                            {
                                LogID = Convert.ToInt32(reader["LogID"]),
                                ActionType = reader["ActionType"].ToString(),
                                EntityName = reader["EntityName"].ToString(),
                                EntityID = Convert.ToInt32(reader["EntityID"]),
                                Timestamp = Convert.ToDateTime(reader["Timestamp"]),
                                Description = reader["Description"].ToString()
                            };
                            logs.Add(log);
                        }
                    }
                }
            }
            return logs;
        }

        // 删除特定日志记录
        public void DeleteLog(int logID)
        {
            using (var conn = new SQLiteConnection(_connectionString))
            {
                conn.Open();
                string query = "DELETE FROM Log WHERE LogID = @logID";
                using (var cmd = new SQLiteCommand(query, conn))
                {
                    cmd.Parameters.AddWithValue("@logID", logID);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        // 清空日志表
        public void ClearLogs()
        {
            using (var conn = new SQLiteConnection(_connectionString))
            {
                conn.Open();
                string query = "DELETE FROM Log";
                using (var cmd = new SQLiteCommand(query, conn))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}
